package com.cloudsigns.factorymanager2.produce.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceAbnormalFeedbackFragment;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceAbnormalReplyFragment;
import com.cloudsigns.factorymanager2.produce.fragment.ProduceNormalFeedbackFragment;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;

import java.util.ArrayList;

public class ProduceEditFeedbackActivity extends FragmentActivity implements View.OnClickListener{
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_normal_feedback,tv_abnormal_feedback,tv_abnormal_reply;
    private CustomViewPager viewpager;

    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;
    public static final int NORMAL_FEEDBACK = 0;
    public static final int ABNORMAL_FEEDBACK = 1;
    public static final int ABNORMAL_REPLY = 2;
    public int currentIndex = NORMAL_FEEDBACK;

    private ProduceNormalFeedbackFragment normalFeedbackFragment;
    private ProduceAbnormalFeedbackFragment abnormalFeedbackFragment;
    private ProduceAbnormalReplyFragment abnormalReplyFragment;

    // ProduceEditFeedbackAdpater

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_produce_edit_feedback);

        initView();
        initData();
    }

    protected void initView() {
        rl_back=findViewById(R.id.rl_back);
        tv_title=findViewById(R.id.tv_title);
        tv_normal_feedback=findViewById(R.id.tv_normal_feedback);
        tv_abnormal_feedback=findViewById(R.id.tv_abnormal_feedback);
        tv_abnormal_reply=findViewById(R.id.tv_abnormal_reply);
        viewpager = (CustomViewPager) findViewById(R.id.viewpager);

        rl_back.setOnClickListener(this);
        tv_normal_feedback.setOnClickListener(this);
        tv_abnormal_feedback.setOnClickListener(this);
        tv_abnormal_reply.setOnClickListener(this);
    }

    protected void initData() {
        tv_title.setText(getResources().getString(R.string.edit_feedback));

        normalFeedbackFragment=new ProduceNormalFeedbackFragment();
        abnormalFeedbackFragment=new ProduceAbnormalFeedbackFragment();
        abnormalReplyFragment=new ProduceAbnormalReplyFragment();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(normalFeedbackFragment);
        listFragments.add(abnormalFeedbackFragment);
        listFragments.add(abnormalReplyFragment);


        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(3);

        PageIndicatorView indicator = (PageIndicatorView) findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.color_blue_17a5ef));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            case NORMAL_FEEDBACK:
                tv_normal_feedback.setSelected(true);
                tv_abnormal_feedback.setSelected(false);
                tv_abnormal_reply.setSelected(false);

                tv_normal_feedback.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                tv_abnormal_feedback.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_abnormal_reply.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;

            case ABNORMAL_FEEDBACK:
                tv_normal_feedback.setSelected(false);
                tv_abnormal_feedback.setSelected(true);
                tv_abnormal_reply.setSelected(false);

                tv_normal_feedback.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_abnormal_feedback.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                tv_abnormal_reply.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;

            case ABNORMAL_REPLY:
                tv_normal_feedback.setSelected(false);
                tv_abnormal_feedback.setSelected(false);
                tv_abnormal_reply.setSelected(true);

                tv_normal_feedback.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_abnormal_feedback.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_abnormal_reply.setTextColor(getResources().getColor(R.color.color_blue_17a5ef));
                break;

            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_back:
                finish();
                break;
            // 正常反馈
            case R.id.tv_normal_feedback:
                if (currentIndex != NORMAL_FEEDBACK) {
                    currentIndex = NORMAL_FEEDBACK;
                    viewpager.setCurrentItem(NORMAL_FEEDBACK);
                    showSelectedOption(NORMAL_FEEDBACK);
                }
                break;
            // 异常反馈
            case R.id.tv_abnormal_feedback:
                if (currentIndex != ABNORMAL_FEEDBACK) {
                    currentIndex = ABNORMAL_FEEDBACK;
                    viewpager.setCurrentItem(ABNORMAL_FEEDBACK);
                    showSelectedOption(ABNORMAL_FEEDBACK);
                }
                break;
            // 异常回复
            case R.id.tv_abnormal_reply:
                if (currentIndex != ABNORMAL_REPLY) {
                    currentIndex = ABNORMAL_REPLY;
                    viewpager.setCurrentItem(ABNORMAL_REPLY);
                    showSelectedOption(ABNORMAL_REPLY);
                }
                break;
        }
    }
}
